﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class DonViDAL
    {
        PhongMachTuDBDataContext db;
        public List<DONVI> LayDSDonVi()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.DONVIs
                    select s;
            return q.ToList();
        }

        public List<DONVI> timKiem(string maDV,string ten)
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.DONVIs
                    where s.MaDonVi.Contains(maDV) || s.TenDonVi.Contains(ten)
                    select s;
            return q.ToList();
        }
        public bool ThemDonVi(DONVI s)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                DONVI r = new DONVI();
                r.MaDonVi = s.MaDonVi;
                r.TenDonVi = s.TenDonVi;
                db.DONVIs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaDonVi(DONVI s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                DONVI r = db.DONVIs.Single(id => id.MaDonVi == s.MaDonVi);
                r.TenDonVi = s.TenDonVi;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaDonVi(string MaDonVi)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.DONVIs
                             where d.MaDonVi == MaDonVi
                             select d;
                if (delete.Count() > 0)
                {
                    db.DONVIs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
